package com.fdb.efp.nls.service.impl;

import com.fdb.basic.framework.core.base.FrameworkService;
import com.fdb.efp.nls.service.dao.SDicDao;
import com.fdb.efp.nls.service.domain.SDic;
import com.fdb.efp.nls.service.facade.SDicService;
import com.fdb.efp.nls.service.vo.SDicListVO;
import com.fdb.efp.nls.service.vo.SDicVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>ServiceImpl: 系统字典项表				<p>
 * @author fuzm
 * @date 2018-05-03
 */
@Service("sDicService")
public class SDicServiceImpl implements SDicService, FrameworkService {
	
	private static Logger logger = LoggerFactory.getLogger(SDicServiceImpl.class);

	@Autowired
	private SDicDao sDicDao;
	
	/**
	 * 新增操作
	 */
	public int insertSDic(SDicVO inSDicVo) {
		logger.debug("当前新增数据为:"+ inSDicVo.toString());
		int num;
		try {
			SDic sDic = new SDic();
			beanCopy(inSDicVo, sDic);
			num = sDicDao.insertSDic(sDic);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:"+ num);
		return num;
	}

	/**
	 * 删除操作
	 */
	public int deleteByPk(SDicVO inSDicVo) {
		logger.debug("当前删除数据条件为:"+ inSDicVo);
		int num;
		try {
			SDic sDic = new SDic();
			beanCopy(inSDicVo, sDic);
			num = sDicDao.deleteByPk(sDic);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSDicVo +"删除的数据条数为"+ num);
		return num;
	}


	/**
	 * 更新操作
	 */
	public int updateByPk(SDicVO inSDicVo) {
		logger.debug("当前修改数据为:"+ inSDicVo.toString());
		int num;
		try {
			SDic sDic = new SDic();
			beanCopy(inSDicVo, sDic);
			num = sDicDao.updateByPk(sDic);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:"+ inSDicVo +"修改的数据条数为"+ num);
		return num;
	}

	//根据opttype和enname查询字典
	@Override
	public SDicVO queryOneByennameOpttype(SDicVO inSDicVo) {
		logger.debug("当前opttype和enname为:"+ inSDicVo.toString());
		SDicVO result = new SDicVO();
		try {
			SDic sDic = new SDic();
			beanCopy(inSDicVo, sDic);
			SDic sDic1 = sDicDao.queryOneByennameOpttype(sDic);
			if (sDic1!=null)
				beanCopy(sDic1,result);
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		logger.debug("根据条件:"+ inSDicVo +"查询的字典为"+ result);
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SDicListVO> queryByopttype(SDicListVO sDicVo) {
			SDicListVO sDicListVO = new SDicListVO();
        List<SDicListVO> sDicListVOs = new ArrayList<>();
		logger.debug("当前查询参数信息为:"+ sDicVo);
		try {
			SDic querySDic = new SDic();
			beanCopy(sDicVo, querySDic);
			List<SDic> queryRslSDic = sDicDao.queryByopttype(querySDic);
			if (Objects.nonNull(queryRslSDic)&& !queryRslSDic.isEmpty()) {
				List<SDicVO> outSDicVo = (List<SDicVO>) beansCopy(queryRslSDic, SDicVO.class);
				logger.debug("当前查询结果为:"+ outSDicVo.toString());
                sDicListVO.setOpttype(querySDic.getOpttype());
                sDicListVO.setsDicVOs(outSDicVo);
                sDicListVOs.add(sDicListVO);
                return sDicListVOs;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

    @SuppressWarnings("unchecked")
	@Override
	public List<SDicListVO> queryByopttypes(List<SDicListVO> sDicVos) {
        List<SDicListVO> sDicListVOs = new ArrayList<>();
		logger.debug("当前查询参数信息为:"+ sDicVos);
		try {
			for (SDicListVO sDicVO : sDicVos){
                SDicListVO sDicListVO = new SDicListVO();
				SDic querySDic = new SDic();
				beanCopy(sDicVO, querySDic);
				List<SDic> queryRslSDic = sDicDao.queryByopttype(querySDic);
				if (Objects.nonNull(queryRslSDic)&& !queryRslSDic.isEmpty()) {
					List<SDicVO> outSDicVo = (List<SDicVO>) beansCopy(queryRslSDic, SDicVO.class);
					logger.debug("当前查询结果为:"+ outSDicVo.toString());
                    sDicListVO.setOpttype(sDicVO.getOpttype());
                    sDicListVO.setsDicVOs(outSDicVo);
                    sDicListVOs.add(sDicListVO);
				} else {
					logger.debug("当前查询结果为空!");
				}
			}
            return sDicListVOs;
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}


	/**
	 * 查询用户权限数据
	 */
	@SuppressWarnings("unchecked")
	public List<SDicVO> queryAllOwner(SDicVO sDicVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SDic sDic = beanCopy(sDicVo, new SDic());
		List<SDicVO> list = null;
		try {
			List<SDic> sDics = sDicDao.queryAllOwnerByPage(sDic);
			logger.debug("当前查询本人所属数据信息的结果集数量为:"+ sDics.size());
			pageSet(sDics, sDic);
			list = (List<SDicVO>) beansCopy(sDics, SDicVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return list;
	
	}
	
	@Override
	public SDicVO queryCnname(SDicVO sDicVO) {
		logger.debug("当前opttype和enname为:"+ sDicVO.toString());
		SDicVO query_cname = null;
		try {
			SDic q_cname = sDicDao.queryCnname(sDicVO);
			query_cname = beanCopy(q_cname,new SDicVO());
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		
		return query_cname;
	}
}
